package com.ximi.ximimall.authcenter.component;

import com.ximi.ximimall.authcenter.domain.LitemallUserDetails;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 西米的风
 * @description TODO
 * @date 2022-10-16 11:50
 */
public class XimiTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        LitemallUserDetails litemallUserDetails = (LitemallUserDetails) authentication.getPrincipal();

        final Map<String, Object> additionalInfo = new HashMap<>();

        final Map<String, Object> retMap = new HashMap<>();

        //todo 这里暴露memberId到Jwt的令牌中,后期可以根据自己的业务需要 进行添加字段
        additionalInfo.put("memberId",litemallUserDetails.getLitemallUser().getId());
        additionalInfo.put("nickName",litemallUserDetails.getLitemallUser().getNickname());
        additionalInfo.put("userLevel",litemallUserDetails.getLitemallUser().getUserLevel());

        retMap.put("additionalInfo",additionalInfo);

        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(retMap);

        return accessToken;
    }
}
